Note: There are often multiple ways to answer each question.
x
and the character string “22” to y
.x <- "11"
y <- "22"
x + y
? Why does R return this result?x + y
## Error in x + y: non-numeric argument to binary operator
Both x
and y
are character variables, and R does not have an in-built way of using the +
operator to add character variables together.
x
and y
as numbers and add them, how could we do that?as.numeric(x) + as.numeric(y)
## [1] 33
z
. Compute the square root of z
.z <- as.numeric(x) + as.numeric(y)
sqrt(z) # soln 1
## [1] 5.744563
z^0.5 # soln 2
## [1] 5.744563
rm(list = ls())
print(STATS_32)
## Error in print(STATS_32): object 'STATS_32' not found
R interprets STATS_32
as the name of a variable and since it does not exist, it complains, saying it did not find an object/variable named STATS_32
. Jack should have put quotation marks around STATS_32
instead:
print("STATS_32")
## [1] "STATS_32"
NA + 2
? Why do you think that is?NA + 2
## [1] NA
We can think of NA
as a placeholder for “I don’t know”. If we add 2 to “I don’t know”, we still don’t know the result!